package com.lovo.ssc.controller;




import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lovo.csc.service.IAuditService;
import com.lovo.psc.service.IPscUserService;
import com.lovo.ssc.entity.Providorder;
import com.lovo.ssc.entity.SysGoods;
import com.lovo.ssc.entity.SysGoods2;
import com.lovo.ssc.entity.SysStore;
import com.lovo.ssc.service.IProvidorderService;
import com.lovo.ssc.service.ISysGoods2Service;
import com.lovo.ssc.service.ISysGoodsService;
import com.lovo.ssc.service.ISysStoreService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * <p>
 * 供应单 前端控制器
 * </p>
 *
 * @author lovo
 * @since 2022-07-08
 */
@RestController
@RequestMapping("/providorder")
@Api(tags = "供货单控制器-张海军-002")
public class ProvidorderController {
    //1.供货单
    @Autowired
    IProvidorderService service;
    //2.总库存
    @Autowired
    ISysGoods2Service sysGoods2Service;
    //3.入库
    @Autowired
    ISysStoreService sysStoreService;
    //4.分批次库存表
    @Autowired
    ISysGoodsService sysGoodsService;
    //5.供应商远程接口
    @Autowired
    IPscUserService pscUserService;

    //6.审核系统远程接口
    @Autowired
    IAuditService auditService;


    /**
     * 1.查询全部供货单
     * @return
     */
    @ApiOperation(value = "查询全部供货单")
    @GetMapping("findAllProvideOrder")
    public List<Providorder> findAllProvideOrder() {
        List<Providorder> list=   service.list();
        return list;
    }

    /**
     * 2.供货单操作
     *传入退货时，feign调用审核和供应商
     *传入入库时，新增入库记录、批次库存表、总库存表、feign调用审核
     * @return
     */
    @ApiOperation(value = "供货单操作")
    @PutMapping("updateState/{id}/{state}")
    public String updateState(@PathVariable("id")Integer id, @PathVariable("state")String state) {
        String goodsCode;
        //修改供货单状态
      Providorder providorder= service.getById(id);
      providorder.setState(state);
      service.saveOrUpdate(providorder);
        //判断选择的状态
        //"3"表示选择入库
        if("已入库".equals(state)){

            //3.总库存加记录(新品需创建商品编号)
            QueryWrapper qw=new QueryWrapper();
            qw.eq("type",providorder.getType());
            qw.eq("specs",providorder.getSpecs());
            qw.eq("goodsName",providorder.getGoodsName());
            SysGoods2 sysGoods2=sysGoods2Service.getOne(qw);
            if(sysGoods2!=null){
                goodsCode=sysGoods2.getCode();
                sysGoods2.setStockNum(sysGoods2.getStockNum()+providorder.getNum());
                //最新采购价
                sysGoods2.setAveInPrice(providorder.getInPrice());
                sysGoods2Service.saveOrUpdate(sysGoods2);
            }else {
                SysGoods2 sysGoods21=new SysGoods2();
                sysGoods21.setType(providorder.getType());
                sysGoods21.setSpecs(providorder.getSpecs());
                sysGoods21.setGoodsName(providorder.getGoodsName());
                sysGoods21.setStockNum(providorder.getNum());
                sysGoods21.setAveInPrice(providorder.getInPrice());
                sysGoods2Service.save(sysGoods21);
                sysGoods21.setCode("sp"+sysGoods21.getId());
                sysGoods2Service.saveOrUpdate(sysGoods21);
                goodsCode=sysGoods21.getCode();
            }
            //1.新增入库记录
            SysStore sysStore=new SysStore();
sysStore.setGoodsCode(goodsCode);
sysStore.setGoodsName(providorder.getGoodsName());
sysStore.setNum(providorder.getNum());
            //时间格式
            DateTimeFormatter dtf =DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
            String format = dtf.format(LocalDateTime.now());
sysStore.setStoreDate(format);
sysStore.setBatch(providorder.getBatch());
sysStore.setOperation("采购入库");
sysStore.setType(providorder.getType());
sysStore.setSpecs(providorder.getSpecs());
sysStoreService.save(sysStore);
            //2.批次库存加记录
            SysGoods sysGoods=new SysGoods();
            sysGoods.setCode(goodsCode);
            sysGoods.setGoodsName(providorder.getGoodsName());
            sysGoods.setStockNum(providorder.getNum());
            sysGoods.setBatch(providorder.getBatch());
            sysGoods.setType(providorder.getType());
            sysGoods.setSpecs(providorder.getSpecs());
            sysGoodsService.save(sysGoods);
            //4.调用审核feign
            auditService.updateOrderState(providorder.getCode(),"2","张三丰");
        }else {//退货操作
         //1.调用审核feign
            auditService.updateOrderState(providorder.getCode(),"3","张三丰");
        //2.调用供应商feign
            pscUserService.updateState(providorder.getCode(),"3");

        }


        return "ok";
    }


}
